﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CLEX.AdminUI.Service;

namespace CLEX.AdminUI.ViewModel
{
    internal sealed class UserEditPasswordVM : ViewModelBase
    {
        private readonly Facade facade;
        private readonly UserData user;
        private RelayCommand cancel;

        public string Username
        {
            get { return user.Username; }
        }

        public RelayCommand Cancel
        {
            set
            {
                if (cancel == null)
                {
                    cancel = new RelayCommand(DoCancel);
                } 
                cancel = value;
            }
        }

        public UserEditPasswordVM(Facade facade, UserData user)
        {
            this.facade = facade;
            this.user = user;
        }

        public void DoChangePassword(string oldPassword, string newPassword)
        {
            try
            {
                facade.ChangeUserPassword(user.UserId, oldPassword, newPassword);
            }
            catch (Exception ex)
            {
                facade.ShowError("An unexpected error occured while updating the user.", ex);
                return;
            }
        }

        private void DoCancel()
        {
            FireCloseRequested();
        }
    }
}
